/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: MainActivity.java
 * 		Description	: Main Screen
 * 		Called		: SplashActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application;

import android.util.Log;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Fragment;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.graphics.Color;
import android.content.Intent;
import android.app.AlertDialog;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.app.FragmentManager;
import android.content.res.TypedArray;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.support.v4.widget.DrawerLayout;
import android.graphics.drawable.ColorDrawable;
import com.kompas.bola.application.R;
import android.support.v4.app.ActionBarDrawerToggle;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.kompas.bola.application.adapter.NavDrawerListAdapter;
import com.kompas.bola.application.analytics.FlurryUtil;
import com.kompas.bola.application.analytics.GAnalyticsUtil;
import com.kompas.bola.application.fragment.BookmarkFragment;
import com.kompas.bola.application.fragment.KanalFragment;
import com.kompas.bola.application.fragment.MainFragment;
import com.kompas.bola.application.ui.NavDrawerItem;
import com.kompas.bola.application.utils.Constants;
import com.comscore.analytics.comScore;

public class MainActivity extends Activity{

	/** 
	 * Drawer */
	private DrawerLayout _drawerLayout;
	private ListView _drawerListView;
	private ActionBarDrawerToggle _drawerToggle;
	private String[] _navMenuTitles;
	private TypedArray _navMenuIcons;
	private ArrayList<NavDrawerItem> _navDrawerItems;
	private NavDrawerListAdapter _drawerAdapter;
	private TextView timeStamp;
	private HashMap<String, String> value;
	/** =========================================================================== */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);		
		overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);

		/*timeStamp									= (TextView) findViewById(R.id.update_date);
		timeStamp.setText(TimeUtils.showLongDateString());
		timeStamp.setVisibility(View.VISIBLE);*/		

		/** 
		 * START GOOGLE ANALYTICS */
		value													= new HashMap<String, String>();
		value.put(Fields.HIT_TYPE, "appview");
		value.put(Fields.SCREEN_NAME, Constants.MAIN_SCREEN);
		GAnalyticsUtil.startSessionWithGA(this,value);
		comScore.setAppContext(this.getApplicationContext());
		
		/** 
		 * ============= START : DRAWER ========== */
		_navMenuTitles 								= getResources().getStringArray(R.array.menu_item);
		_navMenuIcons								= getResources().obtainTypedArray(R.array.nav_drawer_icons);
		_drawerLayout								= (DrawerLayout) findViewById(R.id.drawer_layout);
		_drawerListView								= (ListView) findViewById(R.id.list_slidermenu); 
		_navDrawerItems								= new ArrayList<NavDrawerItem>(); 
		
		//Home (Get Latest)
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[0], _navMenuIcons.getResourceId(0, -1)));
		//Jadwal (Get Latest)
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[1], _navMenuIcons.getResourceId(1, -1)));
		//Home (Get Latest)
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[2], _navMenuIcons.getResourceId(2, -1)));
		//Liga Champion
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[3], _navMenuIcons.getResourceId(3, -1)));
		//Liga Indonesia
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[4], _navMenuIcons.getResourceId(4, -1)));
		//Liga Inggris
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[5], _navMenuIcons.getResourceId(5, -1)));
		//Liga Italia
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[6], _navMenuIcons.getResourceId(6, -1)));
		//Liga Jerman
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[7], _navMenuIcons.getResourceId(7, -1)));
		//Liga Spanyol
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[8], _navMenuIcons.getResourceId(8, -1)));
		//Bookmark
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[9], _navMenuIcons.getResourceId(10, -1)));
		//Klasemen
		_navDrawerItems.add(new NavDrawerItem(_navMenuTitles[10], _navMenuIcons.getResourceId(11, -1)));
		//BPL
		_navDrawerItems.add(new NavDrawerItem(Constants.BPL_TITLE, _navMenuIcons.getResourceId(11, -1)));
		//Serie - A
		_navDrawerItems.add(new NavDrawerItem(Constants.SERIA_A_TITLE, _navMenuIcons.getResourceId(11, -1)));
		//La Liga
		_navDrawerItems.add(new NavDrawerItem(Constants.LA_LIGA_TITLE, _navMenuIcons.getResourceId(11, -1)));
		//Bundesliga
		_navDrawerItems.add(new NavDrawerItem(Constants.BUNDESLIGA_TITLE, _navMenuIcons.getResourceId(11, -1)));
		
		_navMenuIcons.recycle();
		_drawerListView.setOnItemClickListener(new SlideMenuClickListener());
		_drawerAdapter 								= new NavDrawerListAdapter(getApplicationContext(),_navDrawerItems);
		_drawerListView.setAdapter(_drawerAdapter);		
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
		getActionBar().setLogo(getResources().getDrawable(R.drawable.logo));

		
		_drawerToggle 								= new ActionBarDrawerToggle(this, _drawerLayout,
				R.drawable.ic_drawer,0,0
		) {
			public void onDrawerClosed(View view) {
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				invalidateOptionsMenu();
			}
		};

		_drawerLayout.setDrawerListener(_drawerToggle);

		if (savedInstanceState == null) {
			displayView(0);
		}
		/** 
		 * ============== END : DRAWER =========== */
	}	
	/** =========================================================================== */
	/**
	 * Slide menu item click listener
	 * */
	private class SlideMenuClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			displayView(position);
		}
	}
	/** =========================================================================== */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	/** =========================================================================== */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (_drawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	/** =========================================================================== */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		boolean drawerOpen 							= _drawerLayout.isDrawerOpen(_drawerListView);
		menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}
	/** =========================================================================== */
	/**
	 * Diplaying fragment view for selected nav drawer list item
	 * */
	private void displayView(int position) {
		Fragment fragment 							= null;
		Intent klasemen 							= new Intent(MainActivity.this,KlasemenActivity.class);
		Intent jadwal	 							= new Intent(MainActivity.this,JadwalActivity.class);
		switch (position) {
		case 0:
			fragment 								= new MainFragment();
			getActionBar().setTitle(Constants.KOMPAS_BOLA);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.MAIN_SCREEN);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 1:
			jadwal.putExtra(Constants.LIGA, Constants.AGENDA);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.AGENDA);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			_drawerLayout.closeDrawer(_drawerListView);
			startActivityForResult(jadwal, 0);
			break;
		case 2:
			fragment 								= new KanalFragment(Constants.ligachampions);
			getActionBar().setTitle(Constants.LIGA_CHAMPIONS);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_CHAMPIONS);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 3:
			fragment 								= new KanalFragment(Constants.ligaindonesia);
			getActionBar().setTitle(Constants.LIGA_INDONESIA);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_INDONESIA);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 4:
			fragment 								= new KanalFragment(Constants.ligainggris);
			getActionBar().setTitle(Constants.LIGA_INGGRIS);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_INGGRIS);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 5:
			fragment 								= new KanalFragment(Constants.ligaitalia);
			getActionBar().setTitle(Constants.LIGA_ITALIA);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_ITALIA);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 6:
			fragment 								= new KanalFragment(Constants.ligajerman);
			getActionBar().setTitle(Constants.LIGA_JERMAN);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_JERMAN);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 7:
			fragment 								= new KanalFragment(Constants.ligaspanyol);
			getActionBar().setTitle(Constants.LIGA_SPANYOL);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_SPANYOL);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 8:
			fragment 								= new KanalFragment(Constants.ligalain);
			getActionBar().setTitle(Constants.LIGA_LAIN);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIGA_LAIN);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 9:
			fragment 								= new BookmarkFragment();
			getActionBar().setTitle(Constants.LIST_BOOKMARK);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LIST_BOOKMARK);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			break;
		case 11:
			klasemen.putExtra(Constants.LIGA, Constants.BPL);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.BPL);
			GAnalyticsUtil.startSessionWithGA(this,value);
						
			_drawerLayout.closeDrawer(_drawerListView);
			startActivityForResult(klasemen, 0);
			break;
		case 12:
			klasemen.putExtra(Constants.LIGA, Constants.SERIA_A);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.SERIA_A);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			_drawerLayout.closeDrawer(_drawerListView);
			startActivityForResult(klasemen, 0);
			break;
		case 13:
			klasemen.putExtra(Constants.LIGA, Constants.LA_LIGA);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.LA_LIGA);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			_drawerLayout.closeDrawer(_drawerListView);
			startActivityForResult(klasemen, 0);
			break;
		case 14:
			klasemen.putExtra(Constants.LIGA, Constants.BUNDESLIGA);

			/** 
			 * START GOOGLE ANALYTICS */
			value									= new HashMap<String, String>();
			value.put(Fields.HIT_TYPE, "appview");
			value.put(Fields.SCREEN_NAME, Constants.BUNDESLIGA);
			GAnalyticsUtil.startSessionWithGA(this,value);
			
			_drawerLayout.closeDrawer(_drawerListView);
			startActivityForResult(klasemen, 0);
			break;

		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager 		= getFragmentManager();
			fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

			_drawerListView.setItemChecked(position, true);
			_drawerListView.setSelection(position);
			setTitle(_navMenuTitles[position]);
			_drawerLayout.closeDrawer(_drawerListView);
		} else {
			Log.e("MainActivity", "Error in creating fragment");
		}
	}
	/** =========================================================================== */
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		_drawerToggle.syncState();
	}
	/** =========================================================================== */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		_drawerToggle.onConfigurationChanged(newConfig);
	}
	/** =========================================================================== */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(R.string.really_quit)
                    .setPositiveButton(R.string.yes,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    finish();
                        			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
                                }

                            }).setNegativeButton(R.string.no, null).show();

            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
	/** =========================== s : Google Analytics ========================== */
	@Override
	public void onStart() {
		super.onStart();		

		EasyTracker.getInstance(this).activityStart(this);  // Add this method.	
	    FlurryUtil.startSessionWithFlurry(this, Constants.MAIN_SCREEN);
		comScore.onEnterForeground();
	}
	/** =========================================================================== */
	@Override
	public void onStop() {
		super.onStop();

	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	    FlurryUtil.endSessionWithFlurry(this,  Constants.MAIN_SCREEN);
		comScore.onExitForeground();
	}
	/** =========================== e : Google Analytics ========================== */
}